<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>子访问父-$parent</title>
</head>

<body>
    <div id="app">
        <cpn ref="cpn"></cpn>
    </div>

    <template id="cpn">
        <div>
            <h2>我是子组件</h2>
            <button @click="getParent">按钮</button>
        </div>
    </template>

    <script src="../vue/vue.js"></script>
    <script>
        const cpn = {
            template: '#cpn',
            data() {
                return {

                }
            },
            props: {},
            methods: {
                getParent() {
                //使用较少
                    //通过$parent访问父组件
                    console.log(this.$parent);

                    //访问根组件:通过$root
                    console.log(this.$root);
                }
            },
        }
        const app = new Vue({
            el: '#app',
            data: {

            },
            components: {
                cpn
            },
            methods: {

            },
        });
    </script>
</body>

</html>